﻿using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NewsCollection.Common.Service;
using System.Windows.Forms;
using NewsCollection.Common.UpdateGUI;
namespace NewsCollection.ListWeb
{
    class Afamilyvn
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public  void Run(ListView lv,Button btnStart,Button btnStop,int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();
           
         
            string urlGoc = "http://afamily.vn";
           
         
            ListCate = load.LoadCatesNoSub("http://afamily.vn/dep.chn", "//ul[@class='mgt5']//li", urlGoc,manuId,"//a");


            int dem = 0; 



            for (int i = 0; i < ListCate.Count(); i++)
            {
          //      ListPage = load.LoadPage(ListCate[i], "//div[@class='paging fl mgb20']//a", urlGoc);

               
                    // featured news
                  
                    
                           List<NewsItemModel> ListFeaturedNews = new List<NewsItemModel>();
                           var loadFeatured1 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='sub_hot fl']//div[@class='sub_hotct fl']", "//h2//a", "", "//h2//a", urlGoc);
                           var loadFeatured2 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='sub_hotct2 fr']//ul//li", "//h3", "", "//h3//a", urlGoc);
                         ListFeaturedNews.AddRange(loadFeatured1);
                         ListFeaturedNews.AddRange(loadFeatured2);

                         for (int k = 0; k < ListFeaturedNews.Count(); k++)
                         {
                             var item = load.LoadNews(ListFeaturedNews[k], "//div[@class='detail_content fl mgt15']", "//h2[@class='sapo fl mgt10 mgb10']", manuId, 0, "//div[@class='detail_content fl mgt15']//img", urlGoc,true);
                             if (item != null)
                             {
                                 dem++;
                                 item.Hot = "Tin hot";
                                 load.AddItemToListView(item);
                                 pdb.SaveImgFeatured(folder, item);
                             }

                             //if (dem == 30)
                             //    break;
                         }
                    
                    List<NewsItemModel> ListDetail = new List<NewsItemModel>();
                    var load1 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='list-news1']//div[@class='box-x2']", "//div//h2//a", "", "//div//h2//a", urlGoc);
                    var load2 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='list-news1']//div[@class='box-x1']", "//h2//a", "", "//a", urlGoc);
                    var load3 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='list-news1']//div[@class='box-x3']", "//div//h2//a", "", "//div//h2//a", urlGoc);


                    ListDetail.AddRange(load1);
                    ListDetail.AddRange(load2);
                    ListDetail.AddRange(load3);
                    for (int k = 0; k < ListDetail.Count(); k++)
                    {
                        var item = load.LoadNews(ListDetail[k], "//div[@class='detail_content fl mgt15']", "//h2[@class='sapo fl mgt10 mgb10']", manuId, 0, "//div[@class='detail_content fl mgt15']//img", urlGoc,false);
                        if (item != null)
                        {
                            dem ++;
                            load.AddItemToListView(item);
                            pdb.SaveImgNormal(folder, item);

                        }

                        //if (dem == 30)
                        //    break;
                    }
                  
                  

                    //if (dem == 30)
                    //    break;
                
                //if (dem == 30)
                //    break;

            }
         //   System.Windows.Forms.MessageBox.Show("\nThanh cong!Co "+dem  +" tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
